package com.example.springboot.controller;

import com.example.springboot.pojo.Notice;
import com.example.springboot.pojo.PageBean;
import com.example.springboot.pojo.Result;
import com.example.springboot.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    NoticeService noticeService;

    @GetMapping("/findById")
    public Result<?> findById(Integer id) {
        Notice notice = noticeService.findById(id);
        return Result.success(notice);
    }

    @PostMapping("/addNotice")
    public Result<?> addNotice(String tittle, String content, String type, Integer status) {
//        System.out.println(content);
        if (tittle == null || (!type.equals("通知") && !type.equals("公告")) || (status != 0 && status != 1)) {
            return Result.error("参数错误");
        }
        noticeService.addNotice(tittle, content, type, status);
        return Result.success();
    }

    @PutMapping("/publish")
    public Result<?> publish(Integer id) {
        Notice notice = noticeService.findById(id);
        if (notice == null) {
            return Result.error("不存在的ID");
        }
        noticeService.publish(id);
        return Result.success();
    }

    @GetMapping("/getList")
    public Result<PageBean<Notice>> getList(Integer pageNum, Integer pageSize) {
        PageBean<Notice> pb = noticeService.getList(pageNum,pageSize);
        return Result.success(pb);
    }

    @PutMapping("/deleteNotice")
    public Result<?> deleteNotice(Integer id) {
        Notice notice = noticeService.findById(id);
        if (notice == null) {
            return Result.error("不存在的ID");
        }
        noticeService.deleteNotice(id);
        return Result.success();
    }

    @PostMapping("/updateNotice")
    public Result<?> updateNotice(Integer id,String tittle,String type,String content){
        Notice notice = noticeService.findById(id);
        if (notice == null) {
            return Result.error("不存在的ID");
        }
        noticeService.updateNotice(id,tittle,type,content);
        return Result.success();
    }

    @GetMapping("/getByType")
    public Result<?> getByType(String type,Integer pageNum, Integer pageSize){
//        System.out.println(type);
        PageBean<Notice> list = noticeService.getByType(type,pageNum,pageSize);
        return Result.success(list);
    }

}
